<?php


namespace broadcast\redis;

use app\Request;
use broadcast\redis\RedisService;
//reids 记录
class RedisRecord
{
    //设置redis
    private $redis;
    //设置当前标识
    private $key;
    //过期时间
    private $overdue;
    //网站标识
    private $website;
    public function __construct ($website,$key) 
    {
        //设置redis
        $this->redis = new RedisService(config('cache')['stores']['single']);
        $this->key = $key;
        $websiteInfo = setRedis::getCache($website,"info");
        if ($websiteInfo['overdue'] != 0) {
            $this->overdue = (int)$websiteInfo['overdue']-time()+(60*60*24*7);
        } else{ 
            $this->overdue = 60*60*24*30;
        }
        $this->website = $website;
    }
    /**
     * 添加讲师纪录
     */ 
    public function addLecturer () 
    {
        //获取当前用户 
        $month = date("Y-m");
        $week =date("Y") . '#' . date('W');
        $day = date("Y-m-d");
        $LecturerRank = setRedis::getCache($this->website,$this->key);
        if ($LecturerRank) {
            $LecturerRank['total'] = $LecturerRank['total']+1;
            //当前月
            if (isset($LecturerRank[$month])) {
                $LecturerRank[$month] =  $LecturerRank[$month]+1;
            } else {
                $LecturerRank[$month] =  1;
            }

            //当前周
            if (isset($LecturerRank[$week])) {
                $LecturerRank[$week] =  $LecturerRank[$week]+1;
            } else {
                $LecturerRank[$week] =  1;
            }

            //当天
            if (isset($LecturerRank[$day])) {
                $LecturerRank[$day] =  $LecturerRank[$day]+1;
            } else {
                $LecturerRank[$day] =  1;
            }
        }else{
            $LecturerRank['total'] = 1;
            $LecturerRank[$month] = 1;
            $LecturerRank[$week] = 1;
            $LecturerRank[$day] = 1;  
        }
        //设置缓存
        setRedis::setCache($this->website,$this->key,$LecturerRank,0);
        return false;
    }

    //获取讲师记录
    public function getLecturers () 
    {
        //获取当前用户 
        $month = date("Y-m");
        $week =date("Y") . '#' . date('W');
        $day = date("Y-m-d");
        
        $data = setRedis::getCache($this->website,$this->key);
        // if (strpos($this->key,'LecturerVoteRecord')!==false) { 
        //     halt($this->key);
        // }
        if ($data) {
            $info = array();
            $info['total'] = isset($data['total']) ? $data['total'] : 0;
            $info["month"] = isset($data[$month]) ? $data[$month] : 0;
            $info["week"] = isset($data[$week]) ? $data[$week] : 0;
            $info["day"] = isset($data[$day]) ? $data[$day] : 0;
            
            return $info;
        }else{
            $info = array();
            $info['total'] = 0;
            $info["month"] = 0;
            $info["week"] = 0;
            $info["day"] = 0;
            return $info;
        }
    }


    //设置 送礼记录
    public function addGivingRecord($amount)
    {
        $time = time();
        $value = $time . "##" . $amount;
        $key = $this->website . ':' . $this->key;
        $save = $this->redis->rPush($key,$value);
        setRedis::expireCache($key,0);
        return $save;
    }

    //获取送礼记录
    public function getGivingRecordList () {
        $key = $this->website . ':' . $this->key;
        $list = $this->redis->lrange($key, -20 ,-1);
        $data = array();
        foreach ($list as $value) {
            $item = explode("##",$value);
            $data = ['time'=>$item[0],'number'=>$item[1]];
        }
        
        return $data;
    }


    //设置红包 
    public function setEnvelopes ($data,$user)
    {
        $time = time();
        $key = $this->website . ':' . $this->key . '##' . $time;
        foreach ($data as $item) {
            $this->redis->rPush($key,$item);
        }
        //设置发红包信息

        $usekey = $this->website . ":" . $this->key ."#:" .$time;
        $this->redis->set($usekey,json_encode($user));
        setRedis::expireCache($key,0);
        return $time;
    }

    //领取红包
    public function getEnvelopes ($time,$uid) 
    {
        $key = $this->website . ':' . $this->key . '##' . $time;
        $usekey = $this->website . ":" . $this->key ."#:" .$time;
        $envelopes = $this->redis->lPop($key);
        $data = $this->redis->get($usekey);
        if ($envelopes) {
            $newData = json_decode($data,true);
            $newData['recordlist'][] = $uid;
            $this->redis->set($usekey,json_encode($newData));
            return ['price' => $envelopes,'userinfo' => $newData];
        }else{
            return ['price' => $envelopes,'userinfo' => json_decode($data,true)];
            $this->redis->del($usekey);
        }
    }
    //获取红包信息
    public function getEnvelopesInfo ($time,$uid) {
        $usekey = $this->website . ":" . $this->key ."#:" .$time;
        $json = $this->redis->get($usekey);
        if (!$json) return '红包不存在';
        $data = json_decode($json,true);
        if (isset($data['recordlist']))
        {
            if (in_array($uid,$data['recordlist'])) {
                return '该红包已领取';
            }else{
                return false;
            }
        }else{
            return false;
        }
        
    }

    //删除红包
    public function delEnvelopes ($time) {
        $key = $this->website . ':' . $this->key . '##' . $time;
        $usekey = $this->website . ":" . $this->key ."#:" .$time;
        $this->redis->del($key);
        $this->redis->del($usekey);
    }

    




    /**
     * 记录key
     * LecturerGiveRecord   点赞key
     * LecturerVoteRecord   投票key
     * useGiftRecordList    送礼记录key
     */

    

}
